package cn.group.controller;

import cn.group.domain.Room;
import cn.group.domain.Teacher;
import cn.group.service.RoomService;
import cn.group.service.TeacherService;
import cn.group.utils.PinyinUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.security.timestamp.TSRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/showTeacher")
    public String getTeacherList(){
        //直接返回jsp页面
        return "teacher";
    }
    @RequestMapping("/getteacher")
    @ResponseBody
    public Map<String,Object> getTeacher(){
        List<Teacher> teachers = teacherService.getAll();
        System.out.println(teachers);
        Map<String,Object>result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取教师信息成功！");
        result.put("teacherList",teachers);
        return result;
    }
    @RequestMapping("/getListBypage")
    @ResponseBody
    public Map<String,Object>getListBypage(Integer currentPage,Integer pageSize){
        PageInfo<Teacher> pageInfo = teacherService.getTeacherByPage(currentPage,pageSize);
        Map<String,Object>result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取教师数据成功");
        result.put("teacherList",pageInfo.getList());//返回查询出来的分页数据
        result.put("totalPage",pageInfo.getPages());//返回当前总共有多少页
        return result;
    }
    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object>edit(Teacher teacher){
        Map<String,Object>result = new HashMap<>();
        boolean modified;
        if (teacher.getId()> 0){
            modified = teacherService.modifyOne(teacher);
        }else {
            String name = teacher.getName();
            String username = PinyinUtils.getPinYin(name);
            teacher.setUsername(username);
            teacher.setPassword("123456");
            modified = teacherService.addOne(teacher);
        }
        if (!modified){
            result.put("code",-1);
            result.put("message","编辑教师数据失败");
        }else {
            result.put("code",0);
            result.put("message","编辑教师数据成功");
        }
        return result;
    }
    @RequestMapping("/getone")
    @ResponseBody
    public Map<String,Object>getone(Integer id){
            Teacher teacher = teacherService.getOneById(id);
            Map<String,Object>result = new HashMap<>();
            result.put("code",0);
            result.put("message","获取教师信息成功！");
            result.put("teacherList",teacher);
            return result;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object>delete(@RequestParam("id") Integer id){
        Map<String,Object> result = new HashMap<>();
        boolean deleted = teacherService.removeOne(id);
        if (!deleted){
            result.put("code",-1);
            result.put("message","删除用户数据失败");
        }else {
            result.put("code",0);
            result.put("message","删除用户数据成功");
        }
        return result;
    }
}
